package cc.linker.steplink.crm.merchanth5.service.redis;

import cc.linker.steplink.crm.merchanth5.dao.MaiBaoLaDao;
import cc.linker.steplink.crm.merchanth5.pojo.MallGoodsSpecPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static com.alibaba.fastjson.JSON.parse;
import static com.alibaba.fastjson.JSON.toJSONString;

/**
 * @Author: CKQ
 * @DateTime: 2020/6/11 18:19
 * @Description: TODO
 */
@Service
public class MerchantService {
    @Autowired
    private MaiBaoLaDao maiBaoLaDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /*
     * 通過商家ID获取商家信息
     * */
    public Map<String, Object> getMerchantDetail(String merchantId, String orgId) {
        Map<String, Object> map = new HashMap<>();
        String merchantInfoStr = stringRedisTemplate.opsForValue().get("merchant:merchant_info_" + (!StringUtils.isEmpty(merchantId) ? merchantId : orgId));
        if (StringUtils.isEmpty(merchantInfoStr)) {
            map = maiBaoLaDao.linkInfo(orgId, merchantId);
            if (map == null) {
                Map<String, Object> temp = new HashMap<>();
                temp.put("storeId",(!StringUtils.isEmpty(merchantId) ? merchantId : ""));
                temp.put("storeLogo", "");
                temp.put("storeName", "");
                temp.put("storeContent", "");
                temp.put("touchPhone", "");
                temp.put("touchMobilePhone", "");
                temp.put("touchProvince", "");
                temp.put("touchCity", "");
                temp.put("touchRegion", "");
                temp.put("touchAddress", "");
                temp.put("businessName", "");
                return temp;
            }
            String businessName = maiBaoLaDao.getBusinessName(orgId, merchantId);
            map.put("businessName", businessName);
            stringRedisTemplate.opsForValue().set("merchant:merchant_info_" + (!StringUtils.isEmpty(merchantId) ? merchantId : orgId), toJSONString(map));
        } else {
            map = (Map<String, Object>) parse(merchantInfoStr);
        }
        map.put("storeId",(!StringUtils.isEmpty(merchantId) ? merchantId : ""));
        return map;
    }

    public Map<String, Object> getMallDetail(String mallsBelongId, String orgId) {
        Map<String, Object> map = maiBaoLaDao.getMallInfo(orgId, mallsBelongId);
        if (map == null) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("storeLogo", "");
            temp.put("storeName", "");
            temp.put("storeContent", "");
            temp.put("touchPhone", "");
            temp.put("touchMobilePhone", "");
            temp.put("touchProvince", "");
            temp.put("touchCity", "");
            temp.put("touchRegion", "");
            temp.put("touchAddress", "");
            temp.put("businessName", "");
            return temp;
        }
        return map;
    }

    public Integer getCouponGoodsType(String couponId) {
        return maiBaoLaDao.getCouponGoodsType(couponId);
    }


}
